﻿using System;
using System.Collections.Generic;
using System.Text;
using Mang24k99.Model.COMM;
using Castle.ActiveRecord;
using System.Data;
using Castle.ActiveRecord.Queries;
using Common;
using NHibernate;
using NHibernate.Expression;
using AjaxPro;

namespace Mang24k99.Business.COMM
{
    public static class BKtCommCodeList
    {
        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="codelist"></param>
        /// <returns></returns>
        public static bool Save(MKtCommCodeList codelist)
        {
            try
            {
                MKtCommCodeList.Create(codelist);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }

        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="codelist"></param>
        /// <returns></returns>
        public static bool Update(MKtCommCodeList codelist)
        {
            try
            {
                MKtCommCodeList.Update(codelist);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

       /// <summary>
       /// 删除一条记录，物理删除
       /// </summary>
       /// <returns></returns>
        public static bool Delete(MKtCommCodeList codelist)
        {
            try
            {
                MKtCommCodeList.Delete(codelist);
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

        /// <summary>
        /// 通过typecode查找类型名称
        /// </summary>
        /// <param name="typecode"></param>
        /// <returns></returns>
        public static string GetTypeName(int typecode)
        {
            return MKtCommCodeList.GetTypeName(typecode);
        }

        /// <summary>
        /// 通过类型名称查找符合该类型所有数据
        /// </summary>
        /// <param name="typename"></param>
        /// <returns></returns>
        public static MKtCommCodeList[] GetCodeType(string typename)
        {
            return MKtCommCodeList.GetCodeType(typename);
        }

        /// <summary>
        /// 验证代码编码是否有效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static  string CodeValidate(string code)
        {
            string strvalidate = string.Empty;
            if (code=="")
            {
                strvalidate = "代码编码不能为空";
                return strvalidate;
            }
            if (PageValidate.IsNumber(code)==false)
            {
                strvalidate = "代码编码必须为数字类型！";
                return strvalidate;
            }
            int intReturn = MKtCommCodeList.SelectByProperty("CODE_NO", Int32.Parse(code));
            if (intReturn != 0)
            {
                strvalidate = "代码编码不能重复！";
                return strvalidate;
            }
            strvalidate = "success";
            return strvalidate;
        }

        /// <summary>
        /// 验证中文名称是否有效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string CNameValidate(string cname,int typecode)
        {
            string strvalidate = string.Empty;
            if (cname == "")
            {
                strvalidate = "中文名称不能为空";
                return strvalidate;
            }
            int intReturn = MKtCommCodeList.CNameValidate(cname, typecode);
            if (intReturn != 0)
            {
                strvalidate = "中文名称不能重复！";
                return strvalidate;
            }
            strvalidate = "success";
            return strvalidate;
        }

        /// <summary>
        /// 验证英文名称是否有效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string ENameValidate(string ename)
        {
            string strvalidate = string.Empty;
            if (ename == "")
            {
                strvalidate = "英文名称不能为空";
                return strvalidate;
            }
            //int intReturn = MKtCommCodeList.SelectByProperty("CODE_ENAME", ename);
            //if (intReturn != 0)
            //{
            //    strvalidate = "英文名称不能重复！";
            //    return strvalidate;
            //}
            strvalidate = "success";
            return strvalidate;
        }

        /// <summary>
        /// 返回所有可用的记录条数
        /// </summary>
        /// <returns></returns>
        public static int GetCount(string typename)
        {
            string where = "where codelist.MKtCommCodeType.TYPE_NAME like '%" + typename + "%' ";
            return (int)MKtCommCodeList.GetCount(where);
        }
        /// <summary>
        /// 返回符合条件的记录集合
        /// </summary>
        /// <param name="start"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> FindALLSliced(int start, int max,string typename)
        {
            string where = " where codelist.MKtCommCodeType.TYPE_NAME like '%"+typename+"%' order by codelist.ID";
            return (IList<MKtCommCodeList>)MKtCommCodeList.FindALLSliced(start, max, where);
        }
        /// <summary>
        /// 通过类型编码查询该类型所有数据
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList<MKtCommCodeList> FindByTypeCode(int type)
        {
            string where = " where codelist.MKtCommCodeType.TYPE_CODE="+type ;
            where += " and codelist.IS_VALIDATE=1";
            return (IList < MKtCommCodeList > )MKtCommCodeList.SelectByWhere(where);
        }


        public static IList<MKtCommCodeList> FindAll()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IS_VALIDATE",1),
                Expression.Eq("MKtCommCodeType.TYPE_CODE",315),
            };
            return MKtCommCodeList.SelectAll(criteria);
        }



        public static IList<MKtCommCodeList> FindAll2()
        {
            ICriterion[] criteria = new ICriterion[]
            {
                Expression.Eq("IS_VALIDATE",1),
                Expression.Eq("MKtCommCodeType.TYPE_CODE",316),
            };
            return MKtCommCodeList.SelectAll(criteria);
        }


        public static MKtCommCodeList Find(int? ID)
        {
            return MKtCommCodeList.SelectByPrimaryKey(ID);
        }

    }
}
